import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from '../components/Login.vue';
// import Home from '../components/Home.vue'
// import Welcome from '../components/Welcome.vue'
// import UsersList from '../components/users/UsersList.vue'
// import JobList from '../components/jobs/JobList.vue'
// import JobType from '../components/jobs/JobType.vue'
// import GoodsList from '../components/goods/GoodsList.vue'
// import GoodsType from '../components/goods/GoodsType.vue'
// import Params from '../components/goods/params.vue'
// import Add from '../components/goods/Add.vue'
// import Order from '../components/order/Order.vue'
// import Report from '../components/reports/Reports.vue'
const Login = () => import(/* webpackChunkName: "group-login" */ '../components/Login.vue')
const Home = () => import(/* webpackChunkName: "group-home-wel" */ '../components/Home.vue')
const Welcome = () => import(/* webpackChunkName: "group-home-wel" */ '../components/Welcome.vue')
const UsersList = () => import(/* webpackChunkName: "group-users-jobs" */ '../components/users/UsersList.vue')
const JobList = () => import(/* webpackChunkName: "group-users-jobs" */ '../components/jobs/JobList.vue')
const JobType = () => import(/* webpackChunkName: "group-users-jobs" */ '../components/jobs/JobType.vue')
const GoodsList = () => import(/* webpackChunkName: "group-goods" */ '../components/goods/GoodsList.vue')
const GoodsType = () => import(/* webpackChunkName: "group-goods" */ '../components/goods/GoodsType.vue')
const Params = () => import(/* webpackChunkName: "group-goods" */ '../components/goods/params.vue')
const Add = () => import(/* webpackChunkName: "group-goods" */ '../components/goods/Add.vue')
const Order = () => import(/* webpackChunkName: "group-order" */ '../components/order/Order.vue')
const Report = () => import(/* webpackChunkName: "group-report" */ '../components/reports/Reports.vue')
const MapCom = () => import('../components/map/mapCom.vue')
Vue.use(VueRouter)

const routes = [
  {
    path: '/', redirect: '/login'
  },
  {
    path: '/login', component: Login
  },
  {
    path: '/home', 
    component: Home, 
    redirect: '/welcome',
    children: [
      {path: '/welcome', component: Welcome},
      {path: '/users', component: UsersList},
      {path: '/roles', component: JobList},
      {path: '/rights', component: JobType},
      {path: '/goods', component: GoodsList},
      {path: '/categories', component: GoodsType},
      {path: '/params', component: Params},
      {path: '/goods/add', component: Add},
      {path: '/orders', component: Order},
      {path: '/reports', component: Report},
      {path: '/map', component: MapCom}
    ]
  }
]

const router = new VueRouter({
  routes
})

///挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  if(to.path == '/login') return next();  //要访问的页面是login页面，直接放行
  const token = window.sessionStorage.getItem("token"); //不是login页面，先看看是不是已经登陆过了，有没有token
  if(!token) return next('/login') // 如果token是空，就是说还没有登录，这时候直接强制页面跳转login
  next() //有token 直接放行
})

export default router
